// 根据区域生成图片
export const getCutImage = (info: any, source: any) => {
  const { x, y, width, height } = info;
  console.log(info);

  const img = new Image();
  img.src = source;
  const canvas = document.createElement('canvas');
  const ctx: any = canvas.getContext('2d');
  canvas.width = ctx.width = width;
  canvas.height = ctx.height = height;
  ctx.drawImage(img, -x, -y, window.innerWidth, window.innerHeight);
  return canvas.toDataURL('image/png');
};

export const copyBase64ImageToClipboard = async (base64: string) => {
  const base64Data = base64.split(',')[1];
  const byteCharacters = atob(base64Data);
  const byteArrays = new Uint8Array(byteCharacters.length);
  for (let i = 0; i < byteCharacters.length; i++) {
    byteArrays[i] = byteCharacters.charCodeAt(i);
  }
  const blob = new Blob([byteArrays], { type: 'image/png' });
  if (navigator.clipboard) {
    await navigator.clipboard.write([
      new ClipboardItem({
        'image/png': blob
      })
    ]);
  } else {
    console.log('系统不支持复制图片到剪贴板');
  }
};
